from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from bit_api import *
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time

class AutoRegister:
    def init_seleconnect(id, df):
        # print(data.iat[1,10])



        res = openBrowser(id)
        driverPath = res['data']['driver']
        debuggerAddress = res['data']['http']

        print(driverPath)
        print(debuggerAddress)

        # selenium 连接代码
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_experimental_option("debuggerAddress", debuggerAddress)

        chrome_service = Service(driverPath)
        driver = webdriver.Chrome(service=chrome_service, options=chrome_options)

        # driver.get('https://www.baidu.com/')
        driver.get('https://www.vinted.com/')
        driver.find_element(By.XPATH, '//*[@id="__next"]/div/div/div[1]/header/div/div/div[3]/div/a[1]').click()
        # driver.find_element(By.XPATH,'/html/body/div[44]/div/div/div/div[2]/div/span[1]/span').click()
        driver.find_element(By.CSS_SELECTOR, '[data-testid="auth-select-type--register-email"]').click()
        # print(driver.page_source)
        # realName_input = driver.find_element(By.CSS_SELECTOR,'[name="realName"]')
        # driver.execute_script('arguments!l.value ="Derek"', realName_input)
        time.sleep(1)

        for index, row in df.iterrows():
            print('Index: ', index)
            print('Name: ', row['realName'])

            js = f'document.getElementById("realName").value="'+row['realName']+'"'
            driver.execute_script(js)

            js = f'document.getElementById("login").value="'+row['username']+'"'
            driver.execute_script(js)

            js = f'document.getElementById("email").value="'+row['email']+'"'
            driver.execute_script(js)

            js = f'document.getElementById("password").value="'+row['password']+'"'
            driver.execute_script(js)

            # subscribeToNewsletter
            js = f'document.getElementById("subscribeToNewsletter").checked="true"'
            driver.execute_script(js)

            js = f'document.getElementById("agreeRules").checked="true"'
            driver.execute_script(js)

            print(driver.title)